package com.yanwq.sayhi.controller.club.adapter;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanwq.sayhi.R;
import com.yanwq.sayhi.common.client.MImageLoader;
import com.yanwq.sayhi.common.event.ArticleEvent;
import com.yanwq.sayhi.common.tool.SharedPrefTool;
import com.yanwq.sayhi.controller.club.activity.ClubActivity;
import com.yanwq.sayhi.controller.club.activity.MyClubActivity;
import com.yanwq.sayhi.dao.bean.Club;
import com.yanwq.sayhi.dao.bean.ClubVip;
import com.yanwq.sayhi.widget.MToast;
import com.yanwq.sayhi.widget.anim.AnimCompact;
import com.yanwq.sayhi.widget.anim.RecyclerWrapper;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 作者：yanweiqiang。
 * 日期：2016/6/16 0016。
 */
public class MyClubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ClubVip> mVipList;
    private Fragment mFragment;
    private AnimCompact mAnimCompact;
    private RecyclerWrapper mRecyclerWrapper;

    public MyClubAdapter(Fragment fragment) {
        super();
        mVipList = new ArrayList<>();
        mFragment = fragment;

        mAnimCompact = new AnimCompact(new AnimCompact.CustomAnimators() {
            @Override
            public Animator[] animators(View view) {
                ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
                alpha.setDuration(500);
                alpha.setInterpolator(new LinearInterpolator());
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", .8f, 1f);
                scaleX.setDuration(500);
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", .8f, 1f);
                scaleY.setDuration(500);
                ObjectAnimator translationY = ObjectAnimator.ofFloat(view, "translationY", view.getMeasuredHeight(), 0);
                translationY.setDuration(500);
                return new Animator[]{alpha, scaleX, scaleY};
            }
        });

        mAnimCompact.setStartPosition(0);
        mAnimCompact.setFirstOnly(false);
        mRecyclerWrapper = new RecyclerWrapper();
    }

    public void addHeader(View headerView) {
        mRecyclerWrapper.addHeader(headerView);
    }

    public void addFooter(View footerView) {
        mRecyclerWrapper.addFooter(footerView);
    }

    @Override
    public int getItemCount() {
        return mRecyclerWrapper.getItemCount(mVipList.size());
    }

    @Override
    public int getItemViewType(int position) {
        int type = mRecyclerWrapper.getItemViewType(position, mVipList.size());

        if (type == -1) {
            return 0;
        }

        return type;
    }

    public void refresh(List<ClubVip> vips) {
        mVipList.clear();
        mVipList.addAll(vips);
        notifyDataSetChanged();
    }

    public void load(List<ClubVip> vips) {
        mVipList.addAll(vips);
        notifyDataSetChanged();
    }

    public void getView(ViewHolder holder, final int position) {
        final Context context = holder.itemView.getContext();
        final ClubVip vip = mVipList.get(position);
        final Club club = vip.getClub();
        MImageLoader.displayImage(club.getPhotoThumbUrl(), holder.avatar);
        holder.name.setText(club.getName());
        holder.motto.setText(club.getMotto());
        holder.position.setText(getPositionName(vip.getPosition()));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vip.getPosition() == 0 || vip.getPosition() == 1) {
                    MToast.showShort(context, "你还没有通过审核");
                    return;
                }

                SharedPrefTool.putConfig("mode", "club");
                SharedPrefTool.putConfig("clubId", club.getId());
                SharedPrefTool.putConfig("clubName", club.getName());
                EventBus.getDefault().post(new ArticleEvent(ArticleEvent.MODE_CHANGE));
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (vip.getPosition() == 2) {
                    Intent intent = new Intent(context, MyClubActivity.class);
                    intent.putExtra("club", club);
                    intent.putExtra("position", position);
                    mFragment.startActivityForResult(intent, 0xf1);
                } else {
                    Intent intent = new Intent(context, ClubActivity.class);
                    intent.putExtra("clubVip", vip);
                    intent.putExtra("club", club);
                    intent.putExtra("type", 1);
                    context.startActivity(intent);
                }
                return true;
            }
        });

        mAnimCompact.onBindViewHolder(holder.itemView, position);
    }

    private String getPositionName(int position) {
        switch (position) {
            case 0:
                return "申请加入";
            case 1:
                return "已被拒绝";
            case 2:
                return "boss";
            case 3:
                return "经理";
            case 4:
                return "会员";
        }

        return "";
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_club, parent, false));
        }

        return mRecyclerWrapper.onCreateViewHolder(viewType, mVipList.size());
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        position = mRecyclerWrapper.getOriginPosition(position);

        if (holder instanceof ViewHolder) {
            getView((ViewHolder) holder, position);
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        ImageView avatar;
        TextView name;
        TextView motto;
        TextView position;

        public ViewHolder(View itemView) {
            super(itemView);
            avatar = (ImageView) itemView.findViewById(R.id.club_avatar);
            name = (TextView) itemView.findViewById(R.id.club_name);
            motto = (TextView) itemView.findViewById(R.id.club_motto);
            position = (TextView) itemView.findViewById(R.id.club_position);
        }
    }
}
